﻿// ****************************************************************************'
//  프로그램 : 
//  설    명 : 
//  작성일자 : 2013-05-03
//  작성  자 : Jaeyong Park
//  특이사항 : 
//  수정내용 : 
// ****************************************************************************'

#region Using

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

#endregion

namespace FormPrint
{
	public partial class PrintPreview : Form
	{
		#region "1. 선언부"

		private PrintAction printAction = PrintAction.PrintToPreview;

		/// <summary>
		/// 출력할 이미지
		/// </summary>
		private Image _img = null;

		/// <summary>
		/// 생성자
		/// </summary>
		public PrintPreview()
		{
			InitializeComponent();

			InitControls();
		}

		#endregion

		#region "2. 초기화"

		/// <summary>
		/// 컨트롤 초기화
		/// </summary>
		private void InitControls()
		{
			#region Field Init

			// 2013-05-02 TODO By Jaeyong Park : 기본 여백 설정을 변경하고, 가로 모드를 기본으로 설정
			pd.DefaultPageSettings.Margins = new Margins(20, 20, 20, 20);
			pd.DefaultPageSettings.Landscape = true;
			pd.OriginAtMargins = true;

			pageSetupDialog1.Document = pd;
			pageSetupDialog1.EnableMetric = true;

			printDialog1.Document = pd;
			printDialog1.AllowCurrentPage = true;
			printDialog1.AllowSelection = true;
			printDialog1.AllowSomePages = true;
			printDialog1.UseEXDialog = true;
			printDialog1.AllowPrintToFile = true;
			printDialog1.PrintToFile = false;

			printPreviewControl1.AutoZoom = true;
			printPreviewControl1.Document = pd;

			chkZoomAuto.Checked = printPreviewControl1.AutoZoom;
			chkZoomAuto.CheckedChanged += chkZoomAuto_CheckedChanged;
			chkZoomAuto_CheckedChanged(null, null);
			zoomTrackBarControl1.Properties.AllowUseMiddleValue = true;
			zoomTrackBarControl1.Properties.Minimum = 30;
			zoomTrackBarControl1.Properties.Middle = 100;
			zoomTrackBarControl1.Properties.Maximum = 300;
			zoomTrackBarControl1.Properties.ShowValueToolTip = true;
			zoomTrackBarControl1.ValueChanged += zoomTrackBarControl1_ValueChanged;
			zoomTrackBarControl1_ValueChanged(null, null);

			#endregion

			#region Setup Init

			PageSet = pd.DefaultPageSettings;
			PrinterSet = pd.PrinterSettings;

			#endregion

			#region Event Init

			btnPrint.Click += btnPrint_Click;
			btnPrinter.Click += btnPrinter_Click;
			btnPageSetup.Click += btnPageSetup_Click;

			pd.BeginPrint += pd_BeginPrint;
			pd.PrintPage += pd_PrintPage;

			#endregion
		}

		#endregion

		#region "3. 객체 제어"

		#region "이벤트"

		private void btnPrint_Click(object sender, EventArgs e)
		{
			pd.Print();
		}

		private void btnPrinter_Click(object sender, EventArgs e)
		{
			bool flagPrint = printDialog1.ShowDialog() == DialogResult.OK;

			// 인쇄 버튼을 누르지 않고, 적용만 눌렀을 경우에도 변경사항을 저장
			PrinterSet = printDialog1.PrinterSettings;

			printPreviewControl1.InvalidatePreview();

			if(flagPrint)
			{
				pd.Print();
			}
		}

		private void btnPageSetup_Click(object sender, EventArgs e)
		{
			if(pageSetupDialog1.ShowDialog() == DialogResult.OK)
			{
				PrinterSet = pageSetupDialog1.PrinterSettings;
				PageSet = pageSetupDialog1.PageSettings;

				printPreviewControl1.InvalidatePreview();
			}
		}

		private void pd_BeginPrint(object sender, PrintEventArgs e)
		{
			printAction = e.PrintAction;
		}

		private void pd_PrintPage(object sender, PrintPageEventArgs e)
		{
			if(_img != null)
			{
				Size drawSize = StaticFn.GetPreferSize(_img.Size, e.MarginBounds.Size);

				// pd.OriginAtMargins 때문에 미리보기와 실제 출력시 위치 맞춤
				if(printAction == PrintAction.PrintToPreview)
				{
					e.Graphics.DrawImage(_img, new Rectangle(e.MarginBounds.Location, drawSize)
					                     , new Rectangle(Point.Empty, _img.Size), GraphicsUnit.Pixel);
				} else
				{
					e.Graphics.DrawImage(_img, new Rectangle(Point.Empty, drawSize)
					                     , new Rectangle(Point.Empty, _img.Size), GraphicsUnit.Pixel);
				}
			}
			e.HasMorePages = false;
		}

		private void chkZoomAuto_CheckedChanged(object sender, EventArgs e)
		{
			printPreviewControl1.AutoZoom = chkZoomAuto.Checked;
			zoomTrackBarControl1.Value = (int)(printPreviewControl1.Zoom * 100);
			zoomTrackBarControl1.Enabled = !chkZoomAuto.Checked;
		}

		private void zoomTrackBarControl1_ValueChanged(object sender, EventArgs e)
		{
			if(!printPreviewControl1.AutoZoom)
			{
				printPreviewControl1.Zoom = zoomTrackBarControl1.Value / 100d;
			}
		}

		#endregion

		#endregion

		#region "4. 사용자 정의 함수"

		public void Print(Form frm, bool preview)
		{
			_img = StaticFn.CaptureForm(frm);
			if(preview)
			{
				this.ShowDialog();
			} else
			{
				pd.Print();
				this.Close();
			}
		}

		#endregion

		#region "5. 프로퍼티"

		/// <summary>
		/// 프린터 설정
		/// </summary>
		private PrinterSettings PrinterSet
		{
			get
			{
				return pd.PrinterSettings;
			}
			set
			{
				pd.PrinterSettings = value;
				pageSetupDialog1.PrinterSettings = value;
				printDialog1.PrinterSettings = value;
			}
		}

		/// <summary>
		/// 용지 설정
		/// </summary>
		private PageSettings PageSet
		{
			get
			{
				return pd.DefaultPageSettings;
			}
			set
			{
				pd.DefaultPageSettings = value;
				pageSetupDialog1.PageSettings = value;
			}
		}

		#endregion
	}
}